package com.waiterxiaoyy.service.impl;

import com.waiterxiaoyy.entity.Jsoup;
import com.waiterxiaoyy.entity.Notice;
import com.waiterxiaoyy.entity.Renew;
import com.waiterxiaoyy.entity.User;
import com.waiterxiaoyy.mapper.MailMapper;
import com.waiterxiaoyy.service.CreateUserService;
import com.waiterxiaoyy.util.SendEmail;
import com.waiterxiaoyy.util.ZhaoShengJsoup;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Service
public class CreateUserServiceImpl implements CreateUserService {
    @Autowired
    private MailMapper mailMapper;
    @Autowired
    private SendEmail sendEmail;

    @Override
    public boolean createUser(User user) {
        try {
            if(mailMapper.existUser(user.getUserEmail()) == 1) {
                return false;
            }
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            user.setCreateTime(simpleDateFormat.format(new Date()));
            if(mailMapper.createUser(user) == 1) {
                sendEmail.sendNewUser(user.getUserEmail());
            }
        } catch (Exception e) {
            mailMapper.deleteUser(user.getUserEmail());
            e.printStackTrace();
            return false;
        }
        return true;
    }

    @Override
    public Object select(int i) {
        if(i == 1) {
            List<User> user =  mailMapper.selectUser();
            return user;
        }
        else if(i == 2) {
            List<Notice> notices = mailMapper.selectNotcies();
            return notices;
        }
        else if(i == 3) {
            List<Renew> renews = mailMapper.renew();
            return renews;
        }
        return null;
    }


}
